<?php
ini_set('max_execution_time', 900);
set_time_limit(900);
//ini_set("display_errors",1);
//error_reporting(E_ALL);

require_once('Classes/PHPExcel/IOFactory.php');
$file_name = 'havokaren.xlsx';
$dir = dirname(__FILE__) . "/excelfile/";

	$objPHPExcel = new PHPExcel();
	$objPHPExcel->getProperties()->setCreator("Van Cam")
							 ->setLastModifiedBy("Van Cam")
							 ->setTitle("Havokaren products")
							 ->setSubject("Havokaren")
							 ->setDescription("All Havokaren products from Terminal")
							 ->setKeywords("products");
	
try {
    $url = "http://api.dddadmin.com/StockService.svc?wsdl";
    $client = new SoapClient($url);
	
	$shopid = "855001";
	$myKey	= "vkal29xcjs81lslc8qlxpgg63anxaqk2ixkskxm19m";
	
	$params['clientid'] = "855001";
	$params['psk'] = "vkal29xcjs81lslc8qlxpgg63anxaqk2ixkskxm19m";
	
	
	$articles = $client->GetWebshopArticles($params);
	$strInfo = ($articles->GetWebshopArticlesResult);
	$arrInfo = json_decode($strInfo);
	$i = 2;
	
	$objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue("A1", "KatalogNumber")
            ->setCellValue("B1", "EdbNumber")
            ->setCellValue("C1", "SupplierName")
            ->setCellValue("D1", "ItemGroupName")
			->setCellValue("E1", "Ean")
            ->setCellValue("F1", "Kparam1")
			->setCellValue("G1", "Kparam2")
			->setCellValue("H1", "Vparam1")
			->setCellValue("I1", "Vparam2")
			->setCellValue("J1", "Vparam3")
			->setCellValue("K1", "Vparam4")
			->setCellValue("L1", "Vparam5")
			->setCellValue("M1", "SalesPriceEach");
            

	foreach($arrInfo as $key=>$value){
		$myindex 		= 'A';
		
		$KatalogNumber 	= $value->KatalogNumber;
		$EdbNumber		= $value->EdbNumber;
		$SupplierName 	= $value->SupplierName;
		$ItemGroupName	= $value->ItemGroupName;
		$Supplier 		= $value->Supplier;
		$ItemGroup		= $value->ItemGroup;
		$Ean 			= $value->Ean;
		$Kparam1		= $value->Kparam1;
		$Kparam2 		= $value->Kparam2;
		$Kparam3		= $value->Kparam3;
		$Kparam4 		= $value->Kparam4;
		$Kparam5		= $value->Kparam5;
		$Vparam1 		= $value->Vparam1;
		$Vparam2		= $value->Vparam2;
		$Vparam3 		= $value->Vparam3;
		$Vparam4		= $value->Vparam4;
		$Vparam5 		= $value->Vparam5;
		$Qty			= $value->Qty;
		$SalesPriceEach	= (float) str_replace(',','.',$value->SalesPriceEach);
		$CostpriceEach	= $value->CostpriceEach;
		
		
		$objPHPExcel->setActiveSheetIndex(0)
			->setCellValue($myindex.$i, $KatalogNumber)
			->setCellValue(++$myindex.$i, $EdbNumber)
            ->setCellValue(++$myindex.$i, $SupplierName)
            ->setCellValue(++$myindex.$i, $ItemGroupName)
			->setCellValue(++$myindex.$i, $Ean)
            ->setCellValue(++$myindex.$i, $Kparam1)
            ->setCellValue(++$myindex.$i, $Kparam2)
			->setCellValue(++$myindex.$i, $Vparam1)
			->setCellValue(++$myindex.$i, $Vparam2)
			->setCellValue(++$myindex.$i, $Vparam3)
			->setCellValue(++$myindex.$i, $Vparam4)
			->setCellValue(++$myindex.$i, $Vparam5)
            ->setCellValue(++$myindex.$i, $SalesPriceEach)
			;
			
		$i++;
	}
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	$objWriter->save($dir.$file_name);
	
	header("Content-type: application/force-download"); 
	header('Content-Disposition: inline; filename="' . $dir.$file_name . '"'); 
	header("Content-Transfer-Encoding: Binary"); 
	header("Content-length: ".filesize($dir.$file_name)); 
	header('Content-Type: application/octet-stream'); 
	header('Content-Disposition: attachment; filename="' . $file_name . '"'); 
	readfile($dir.$file_name);
	
}
catch (SoapFault $exception) {
    echo $exception->getMessage();
}
?>